package com.zzg.mybatis.generator.plugins;

import java.util.List;

import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.FullyQualifiedTable;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.Plugin;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.Context;

public class MYSQLQueryPlugin extends PluginAdapter {

	private String orderbystr = "orderStr";
	private String customSqlText = "customQuerySqlText";

	@Override
	public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

		// addLimit(topLevelClass, introspectedTable, "limitStart");
		// addLimit(topLevelClass, introspectedTable, "limitEnd");

		addCustomQueryText(topLevelClass, introspectedTable, customSqlText);
		addOrderColumn(topLevelClass, introspectedTable, orderbystr);

		//
		// FullyQualifiedJavaType interfaceArray = new
		// FullyQualifiedJavaType("Serializable");
		// topLevelClass.addSuperInterface(interfaceArray);
		// topLevelClass.addImportedType("java.io.Serializable");

		// 为WMS系统开发框架做出的扩展
		if (true) {
			FullyQualifiedJavaType superBean = new FullyQualifiedJavaType("BaseValueObject");
			topLevelClass.setSuperClass(superBean);
		}

		return super.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
	}

	/*
	 * public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass
	 * topLevelClass, IntrospectedTable introspectedTable) {
	 * 
	 * topLevelClass.setSuperClass(new
	 * FullyQualifiedJavaType("BaseMapper<Mdepartment, Integer>"));
	 * topLevelClass.addAnnotation("MyBatisRepository"); return
	 * super.modelRecordWithBLOBsClassGenerated(topLevelClass, introspectedTable); }
	 */

	@Override
	public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
		XmlElement isParameterPresenteElemen = document.getRootElement();
		Context context = introspectedTable.getContext();
		FullyQualifiedTable fullyQualifiedTable = introspectedTable.getFullyQualifiedTable();
		
		
		
		
		isParameterPresenteElemen.addElement(getSelectAll(introspectedTable));
		isParameterPresenteElemen.addElement(getSelectAllCount(introspectedTable));

		isParameterPresenteElemen.addElement(getSelectLikeAll(introspectedTable));
		isParameterPresenteElemen.addElement(getSelectLikeAllCount(introspectedTable));

		return super.sqlMapDocumentGenerated(document, introspectedTable);
	}

	@Override
	public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,
			IntrospectedTable introspectedTable) {
		XmlElement isParameterPresenteElemen = (XmlElement) element.getElements().get(element.getElements().size() - 1);
		XmlElement isNotNullElement = new XmlElement("isGreaterEqual"); //$NON-NLS-1$
		isNotNullElement.addAttribute(new Attribute("property", "limitStart")); //$NON-NLS-1$ //$NON-NLS-2$
		isNotNullElement.addAttribute(new Attribute("compareValue", "0")); //$NON-NLS-1$ //$NON-NLS-2$
		isNotNullElement.addElement(new TextElement("limit $limitStart$ , $limitEnd$"));
		isParameterPresenteElemen.addElement(isNotNullElement);
		return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
	}

	@Override
	public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass,IntrospectedTable introspectedTable) {
		// topLevelClass
		String objectName = introspectedTable.getTableConfiguration().getDomainObjectName();// 对象名称
		if (introspectedTable.getPrimaryKeyColumns() == null || introspectedTable.getPrimaryKeyColumns().size() == 0) {
			new Exception("对象" + objectName + "缺少主键");
		}
		String primaryKeyType = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType()
				.getFullyQualifiedName();// 组合主键会有问题

		interfaze.addImportedType(new FullyQualifiedJavaType("java.util.List"));
		interfaze.addImportedType(new FullyQualifiedJavaType("java.lang.Integer"));
		//String = _object_all_path = context.getJavaModelGeneratorConfiguration().getTargetPackage() + "." + introspectedTable.getFullyQualifiedTable().getDomainObjectName());
		String _object_all_path = context.getJavaModelGeneratorConfiguration().getTargetPackage() + "." + introspectedTable.getFullyQualifiedTable().getDomainObjectName();
		interfaze.addImportedType(new FullyQualifiedJavaType(_object_all_path));

		Method method = new Method();//
		method.setName("selectLikeAll");
		method.addParameter(new Parameter(
				new FullyQualifiedJavaType(introspectedTable.getFullyQualifiedTable().getDomainObjectName()), "vo"));
		method.setReturnType(new FullyQualifiedJavaType("java.util.List<" + objectName + ">"));
		interfaze.addMethod(method);

		method = new Method();//
		method.setName("selectLikeAllCount");
		method.addParameter(new Parameter(
				new FullyQualifiedJavaType(introspectedTable.getFullyQualifiedTable().getDomainObjectName()), "vo"));
		method.setReturnType(new FullyQualifiedJavaType("java.lang.Integer"));
		interfaze.addMethod(method);

		method = new Method();//
		method.setName("selectAll");
		method.addParameter(new Parameter(
				new FullyQualifiedJavaType(introspectedTable.getFullyQualifiedTable().getDomainObjectName()), "vo"));
		method.setReturnType(new FullyQualifiedJavaType("java.util.List<" + objectName + ">"));
		interfaze.addMethod(method);

		method = new Method();//
		method.setName("selectAllCount");
		method.addParameter(new Parameter(
				new FullyQualifiedJavaType(introspectedTable.getFullyQualifiedTable().getDomainObjectName()), "vo"));
		method.setReturnType(new FullyQualifiedJavaType("java.lang.Integer"));
		interfaze.addMethod(method);

		// zyf 2016 为WMS系统开发框架做出的扩展
		if (true) {
			//spring 项目继承 mybatis
			interfaze.addAnnotation("@MyBatisRepository");
			//com.lucker.framework.annotation
			interfaze.addImportedType(new FullyQualifiedJavaType("com.lucker.framework.annotation.MyBatisRepository"));
			interfaze.addSuperInterface(new FullyQualifiedJavaType("BaseMapper<" + objectName + ", " + primaryKeyType + ">"));
			
			//spring boot 项目集成 mybatis 用下面的配置
			//interfaze.addAnnotation("@Mapper");
			//interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper"));
			//下面是支持mapper 扩展
			interfaze.addSuperInterface(new FullyQualifiedJavaType("BaseMapper<" + objectName + ", " + primaryKeyType + ">"));
		}

		/* 该行代码的作用：当commentGenerator配置为false时，接口可以生成注释代码。 没有意义，所以注释，其他新加的方法已经删除 */
		// context.getCommentGenerator().addGeneralMethodComment(method,
		// introspectedTable);

		return super.clientGenerated(interfaze, topLevelClass, introspectedTable);
	}

	@Override
	public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass,
			IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable,
			Plugin.ModelClassType modelClassType) {
		// CommentGenerator commentGenerator =
		// introspectedTable.getContext().getCommentGenerator();
		/**
		 * Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC);
		 * method.setName("set" + camel); method.addParameter(new
		 * Parameter(FullyQualifiedJavaType .getIntInstance(), name));
		 * method.addBodyLine("this." + name + " = " + name + ";");
		 * commentGenerator.addGeneralMethodComment(method, introspectedTable);
		 * topLevelClass.addMethod(method);
		 */

		return super.modelSetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable,
				modelClassType);
	}
	
	//不在对象里面添加分页参数，有些过度设计, 利用第三方分页插件实现配置上面会更简单。也不会破坏原有的MODEL概念
	private void addLimit(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) {
		CommentGenerator commentGenerator = context.getCommentGenerator();
		Field field = new Field();
		field.setVisibility(JavaVisibility.PRIVATE);
		field.setType(FullyQualifiedJavaType.getIntInstance());
		field.setName(name);
		field.setInitializationString("-1");
		commentGenerator.addFieldComment(field, introspectedTable);
		topLevelClass.addField(field);

		char c = name.charAt(0);
		String camel = Character.toUpperCase(c) + name.substring(1);
		Method method = new Method();
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setName("set" + camel);
		method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), name));
		method.addBodyLine("this." + name + " = " + name + ";");
		commentGenerator.addGeneralMethodComment(method, introspectedTable);
		topLevelClass.addMethod(method);
		method = new Method();
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setReturnType(FullyQualifiedJavaType.getIntInstance());
		method.setName("get" + camel);
		method.addBodyLine("return " + name + ";");
		commentGenerator.addGeneralMethodComment(method, introspectedTable);
		topLevelClass.addMethod(method);

	}

	private void addCustomQueryText(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) {
		CommentGenerator commentGenerator = context.getCommentGenerator();
		Field field = new Field();
		field.setVisibility(JavaVisibility.PRIVATE);
		field.setType(FullyQualifiedJavaType.getStringInstance());
		field.setName(name);
		field.setInitializationString("null");
		commentGenerator.addFieldComment(field, introspectedTable);
		topLevelClass.addField(field);

		char c = name.charAt(0);
		String camel = Character.toUpperCase(c) + name.substring(1);
		Method method = new Method();
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setName("set" + camel);
		method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), name));
		method.addBodyLine("this." + name + " = " + name + ";");
		commentGenerator.addGeneralMethodComment(method, introspectedTable);
		topLevelClass.addMethod(method);
		method = new Method();
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setReturnType(FullyQualifiedJavaType.getStringInstance());
		method.setName("get" + camel);
		method.addBodyLine("return " + name + ";");
		commentGenerator.addGeneralMethodComment(method, introspectedTable);
		topLevelClass.addMethod(method);

	}

	private void addOrderColumn(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) {
		CommentGenerator commentGenerator = context.getCommentGenerator();
		Field field = new Field();
		field.setVisibility(JavaVisibility.PRIVATE);
		field.setType(FullyQualifiedJavaType.getStringInstance());
		field.setName(name);
		field.setInitializationString("null");
		commentGenerator.addFieldComment(field, introspectedTable);
		topLevelClass.addField(field);

		char c = name.charAt(0);
		String camel = Character.toUpperCase(c) + name.substring(1);
		Method method = new Method();
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setName("set" + camel);
		method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), name));
		method.addBodyLine("this." + name + " = " + name + ";");
		commentGenerator.addGeneralMethodComment(method, introspectedTable);
		// topLevelClass.addMethod(method);
		method = new Method();
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setReturnType(FullyQualifiedJavaType.getStringInstance());
		method.setName("get" + camel);
		method.addBodyLine("return " + name + ";");
		commentGenerator.addGeneralMethodComment(method, introspectedTable);
		// topLevelClass.addMethod(method);
		List<IntrospectedColumn> allColnumnList = introspectedTable.getAllColumns();
		Method orderbyMethod;
		for (IntrospectedColumn col : allColnumnList) {
			orderbyMethod = new Method();

			orderbyMethod.setVisibility(JavaVisibility.PUBLIC);
			String colname = col.getJavaProperty();
			char cs = colname.charAt(0);
			String camels = Character.toUpperCase(cs) + colname.substring(1);
			orderbyMethod.setName("orderBy" + camels);
			orderbyMethod.addParameter(new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "isAsc"));
			orderbyMethod.addBodyLine("if(isAsc){");
			orderbyMethod.addBodyLine("if(this." + orderbystr + " == null){");
			orderbyMethod.addBodyLine("this." + orderbystr + " = \"order by " + col.getActualColumnName() + " asc\";");
			orderbyMethod.addBodyLine("}else{");
			orderbyMethod.addBodyLine(
					"this." + orderbystr + " = this." + orderbystr + " + \"," + col.getActualColumnName() + " asc\";");
			orderbyMethod.addBodyLine("}");
			orderbyMethod.addBodyLine("}else{");
			orderbyMethod.addBodyLine("if(this." + orderbystr + " == null){");
			orderbyMethod.addBodyLine("this." + orderbystr + " = \"order by " + col.getActualColumnName() + " desc\";");
			orderbyMethod.addBodyLine("}else{");
			orderbyMethod.addBodyLine(
					"this." + orderbystr + " = this." + orderbystr + " + \"," + col.getActualColumnName() + " desc\";");
			orderbyMethod.addBodyLine("}");
			orderbyMethod.addBodyLine("}");

			commentGenerator.addGeneralMethodComment(orderbyMethod, introspectedTable);
			topLevelClass.addMethod(orderbyMethod);

		}

	}

	public boolean validate(List<String> warnings) {
		return true;
	}
	// public static void generate() {
	// String config =
	// MYSQLQueryPlugin.class.getClassLoader().getResource("generatorConfig.xml").getFile();
	// String[] arg = { "-configfile", config, "-overwrite" };
	// ShellRunner.main(arg);
	// }

	public void getQueryNodeText3(XmlElement pElement, IntrospectedTable introspectedTable, boolean isPage) {
		XmlElement whereElement = new XmlElement("where");
		List<IntrospectedColumn> allColnumnList = introspectedTable.getAllColumns();
		XmlElement ifElement = null;
		for (IntrospectedColumn col : allColnumnList) {
			ifElement = new XmlElement("if");
			ifElement.addAttribute(new Attribute("test", col.getJavaProperty() + " != null"));
			if (col.getJdbcTypeName() != null && "VARCHAR".equals(col.getJdbcTypeName())) {
				ifElement.addElement(new TextElement(" AND " + col.getActualColumnName() + " like CONCAT('%',#{"
						+ col.getJavaProperty() + ",jdbcType=" + col.getJdbcTypeName() + "},'%')"));
			} else {
				ifElement.addElement(new TextElement(" AND " + col.getActualColumnName() + " = #{"
						+ col.getJavaProperty() + ",jdbcType=" + col.getJdbcTypeName() + "}"));
			}
			whereElement.addElement(ifElement);
		}

		// 因为下面条件是需要AND连接符所以,不能写在下面的分页判断里面，需要写在whereElement里面 ZYF 2016.10
		ifElement = new XmlElement("if");
		ifElement.addAttribute(new Attribute("test", customSqlText + " != null"));
		ifElement.addElement(new TextElement(" AND  ${" + customSqlText + "}"));
		whereElement.addElement(ifElement);

		pElement.addElement(whereElement);

		// 添加分页
		if (isPage) {

			// 添加排序,为了提交效率，不分页的查询不添加排序，
			XmlElement orderbyElement = new XmlElement("if");
			orderbyElement.addAttribute(new Attribute("test", "" + orderbystr + " != null"));
			orderbyElement.addElement(new TextElement("${" + orderbystr + "}"));
			pElement.addElement(orderbyElement);

			XmlElement pageElement = new XmlElement("if");
			pageElement.addAttribute(new Attribute("test", "limitStart != null"));
			pageElement
					.addElement(new TextElement("limit #{limitStart,jdbcType=INTEGER}, #{limitEnd,jdbcType=INTEGER}"));
			pElement.addElement(pageElement);
		}
		// return whereElement;
	}

	public void getQueryNodeText3ForEq(XmlElement pElement, IntrospectedTable introspectedTable, boolean isPage) {
		XmlElement whereElement = new XmlElement("where");
		List<IntrospectedColumn> allColnumnList = introspectedTable.getAllColumns();
		XmlElement ifElement = null;
		for (IntrospectedColumn col : allColnumnList) {
			ifElement = new XmlElement("if");
			ifElement.addAttribute(new Attribute("test", col.getJavaProperty() + " != null"));
			if (col.getJdbcTypeName() != null && "VARCHAR".equals(col.getJdbcTypeName())) {
				// ifElement.addElement(new TextElement(col.getActualColumnName() + " like
				// CONCAT('%',#{"+ col.getJavaProperty() +",jdbcType="+ col.getJdbcTypeName()
				// +"},'%')"));
				ifElement.addElement(new TextElement(" AND " + col.getActualColumnName() + " = #{"
						+ col.getJavaProperty() + ",jdbcType=" + col.getJdbcTypeName() + "}"));
			} else {
				// ifElement.addElement(new TextElement(col.getActualColumnName() + " = #{"+
				// col.getJavaProperty() +",jdbcType="+ col.getJdbcTypeName() +"}"));
				ifElement.addElement(new TextElement(" AND " + col.getActualColumnName() + " = #{"
						+ col.getJavaProperty() + ",jdbcType=" + col.getJdbcTypeName() + "}"));
			}
			whereElement.addElement(ifElement);
		}

		// 因为下面条件是需要AND连接符所以,不能写在下面的分页判断里面，需要写在whereElement里面
		ifElement = new XmlElement("if");
		ifElement.addAttribute(new Attribute("test", customSqlText + " != null"));
		ifElement.addElement(new TextElement(" AND ${" + customSqlText + "}"));
		whereElement.addElement(ifElement);

		pElement.addElement(whereElement);

		// 添加分页
		if (isPage) {
			// 添加排序,为了提交效率，不分页的查询不添加排序，
			XmlElement orderbyElement = new XmlElement("if");
			orderbyElement.addAttribute(new Attribute("test", "" + orderbystr + " != null"));
			orderbyElement.addElement(new TextElement("${" + orderbystr + "}"));
			pElement.addElement(orderbyElement);

			XmlElement pageElement = new XmlElement("if");
			pageElement.addAttribute(new Attribute("test", " limitStart != null"));
			pageElement
					.addElement(new TextElement("limit #{limitStart,jdbcType=INTEGER}, #{limitEnd,jdbcType=INTEGER}"));
			pElement.addElement(pageElement);
		}
		// return whereElement;
	}

	public XmlElement getQueryNodeText2(IntrospectedTable introspectedTable, boolean isPage) {
		XmlElement whereElement = new XmlElement("where");
		List<IntrospectedColumn> allColnumnList = introspectedTable.getAllColumns();
		XmlElement ifElement = null;
		for (IntrospectedColumn col : allColnumnList) {
			ifElement = new XmlElement("if");
			ifElement.addAttribute(new Attribute("test", col.getJavaProperty() + " != null"));
			if (col.getJdbcTypeName() != null && "VARCHAR".equals(col.getJdbcTypeName())) {
				ifElement.addElement(new TextElement(" AND " + col.getActualColumnName() + " like CONCAT('%',#{"
						+ col.getJavaProperty() + ",jdbcType=" + col.getJdbcTypeName() + "},'%')"));
			} else {
				ifElement.addElement(new TextElement(" AND " + col.getActualColumnName() + " = #{"
						+ col.getJavaProperty() + ",jdbcType=" + col.getJdbcTypeName() + "}"));
			}
			whereElement.addElement(ifElement);
		}

		// 添加分页
		if (isPage) {

			// 添加排序,为了提高代码生成效率，不分页的查询不添加排序，
			XmlElement orderbyElement = new XmlElement("if");
			orderbyElement.addAttribute(new Attribute("test", "" + orderbystr + " != null"));
			orderbyElement.addElement(new TextElement("#{" + orderbystr + ",jdbcType=VARCHAR}"));
			whereElement.addElement(orderbyElement);

			XmlElement pageElement = new XmlElement("if");
			pageElement.addAttribute(new Attribute("test", "limitStart != null"));
			pageElement
					.addElement(new TextElement("limit #{limitStart,jdbcType=INTEGER}, #{limitEnd,jdbcType=INTEGER}"));
			whereElement.addElement(pageElement);
		}
		return whereElement;
	}

	public XmlElement getSelectLikeAll(IntrospectedTable introspectedTable) {
		XmlElement isNotNullElement = new XmlElement("select"); //$NON-NLS-1$
		isNotNullElement.addAttribute(new Attribute("id", "selectLikeAll")); //$NON-NLS-1$ //$NON-NLS-2$
		isNotNullElement.addAttribute(new Attribute("resultMap", "BaseResultMap")); //$NON-NLS-1$ //$NON-NLS-2$
		isNotNullElement.addAttribute(
				new Attribute("parameterType", context.getJavaModelGeneratorConfiguration().getTargetPackage() + "." //$NON-NLS-1$ //$NON-NLS-2$
						+ introspectedTable.getFullyQualifiedTable().getDomainObjectName()));
		XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$
		includeElement.addAttribute(new Attribute("refid", "Base_Column_List"));
		isNotNullElement.addElement(new TextElement("select "));
		isNotNullElement.addElement(includeElement);
		isNotNullElement.addElement(new TextElement(
				"from  " + introspectedTable.getFullyQualifiedTable().getAliasedFullyQualifiedTableNameAtRuntime()));
		getQueryNodeText3(isNotNullElement, introspectedTable, true);

		return isNotNullElement;
	}

	public XmlElement getSelectAll(IntrospectedTable introspectedTable) {

		XmlElement isNotNullElement = new XmlElement("select"); //$NON-NLS-1$
		isNotNullElement.addAttribute(new Attribute("id", "selectAll")); //$NON-NLS-1$ //$NON-NLS-2$
		isNotNullElement.addAttribute(new Attribute("resultMap", "BaseResultMap")); //$NON-NLS-1$ //$NON-NLS-2$
		isNotNullElement.addAttribute(
				new Attribute("parameterType", context.getJavaModelGeneratorConfiguration().getTargetPackage() + "." //$NON-NLS-1$ //$NON-NLS-2$
						+ introspectedTable.getFullyQualifiedTable().getDomainObjectName()));
		XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$
		includeElement.addAttribute(new Attribute("refid", "Base_Column_List"));
		isNotNullElement.addElement(new TextElement("select "));
		isNotNullElement.addElement(includeElement);
		isNotNullElement.addElement(new TextElement(
				"from  " + introspectedTable.getFullyQualifiedTable().getAliasedFullyQualifiedTableNameAtRuntime()));
		getQueryNodeText3ForEq(isNotNullElement, introspectedTable, true);

		return isNotNullElement;
	}

	public XmlElement getSelectLikeAllCount(IntrospectedTable introspectedTable) {

		XmlElement isNotNullElement = new XmlElement("select"); //$NON-NLS-1$
		isNotNullElement.addAttribute(new Attribute("id", "selectLikeAllCount")); //$NON-NLS-1$ //$NON-NLS-2$
		isNotNullElement.addAttribute(new Attribute("resultType", "java.lang.Integer")); //$NON-NLS-1$ //$NON-NLS-2$
		isNotNullElement.addAttribute(
				new Attribute("parameterType", context.getJavaModelGeneratorConfiguration().getTargetPackage() + "." //$NON-NLS-1$ //$NON-NLS-2$
						+ introspectedTable.getFullyQualifiedTable().getDomainObjectName()));
		// XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$
		// includeElement.addAttribute(new Attribute("refid", "Base_Column_List"));
		// isNotNullElement.addElement(new TextElement("select "));
		// isNotNullElement.addElement(includeElement);
		isNotNullElement.addElement(new TextElement("select count(1) from  "
				+ introspectedTable.getFullyQualifiedTable().getAliasedFullyQualifiedTableNameAtRuntime()));
		getQueryNodeText3(isNotNullElement, introspectedTable, false);

		return isNotNullElement;

	}

	public XmlElement getSelectAllCount(IntrospectedTable introspectedTable) {
		XmlElement isNotNullElement = new XmlElement("select"); //$NON-NLS-1$
		isNotNullElement.addAttribute(new Attribute("id", "selectAllCount")); //$NON-NLS-1$ //$NON-NLS-2$
		isNotNullElement.addAttribute(new Attribute("resultType", "java.lang.Integer")); //$NON-NLS-1$ //$NON-NLS-2$
		isNotNullElement.addAttribute(new Attribute("parameterType", context.getJavaModelGeneratorConfiguration().getTargetPackage() + "." + introspectedTable.getFullyQualifiedTable().getDomainObjectName()));
		
		
		// XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$
		// includeElement.addAttribute(new Attribute("refid", "Base_Column_List"));
		// isNotNullElement.addElement(new TextElement("select "));
		// isNotNullElement.addElement(includeElement);
		isNotNullElement.addElement(new TextElement("select count(1) from  "
				+ introspectedTable.getFullyQualifiedTable().getAliasedFullyQualifiedTableNameAtRuntime()));
		getQueryNodeText3ForEq(isNotNullElement, introspectedTable, false);
		return isNotNullElement;
	}

	// public static void main(String[] args) {
	// try{
	// generate();
	// }catch(Exception e){
	// e.printStackTrace();
	// }
	//
	// System.out.println("....test...");
	// }

}
